﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace DELVE_GAME.ViewStructure.MapEditor
{
    class MapEditorInitStage : Stage
    {
        String boardTitle = "UNTITLED";
        int maxTitleLength = 32;
        TextComponent titleEditor;
        Map map;
        int mapWidth;
        int mapHeight;
        int maxWidth = (int)Map.DEFAULTS.MAX_XDIMENSION;
        int maxHeight = (int)Map.DEFAULTS.MAX_YDIMENSION;
        TextComponent widthText;
        TextComponent heightText;

        public MapEditorInitStage(Map map, SpriteFont font, Sprite upButtonSprite, Sprite downButtonSprite)
        {
            this.map = map;
            boardTitle = map.name;
            mapWidth = map.board.Length;
            mapHeight = map.board[0].Length;
            titleEditor = new TextComponent(font, 100, 100);
            titleEditor.setString("Map Name: " + boardTitle);
            this.add(titleEditor);
            initWidthAndHeight(font, upButtonSprite, downButtonSprite);
        }

        private void initWidthAndHeight( SpriteFont font, Sprite upButtonSprite, Sprite downButtonSprite)
        {
            GameAction upHeight = new MapHeightUpGameAction(this);
            GameAction downHeight = new MapHeightDownGameAction(this);
            GameAction upWidth = new MapWidthUpGameAction(this);
            GameAction downWidth = new MapWidthDownGameAction(this);

            Button widthUpButton = new Button(200, 200, upButtonSprite);
                widthUpButton.setMouseReleasedAction(upWidth);
            Button widthDownButton = new Button(200, 225, downButtonSprite);
                widthDownButton.setMouseReleasedAction(downWidth);
            Button heightUpButton = new Button(200, 300, upButtonSprite);
                heightUpButton.setMouseReleasedAction(upHeight);
            Button heightDownButton = new Button(200, 325, downButtonSprite);
                heightDownButton.setMouseReleasedAction(downHeight);

            widthText = new TextComponent(font, 50, 205);
            heightText = new TextComponent(font, 50, 305);
            widthText.setString("Width:  " + mapWidth.ToString());
            heightText.setString("Height: " + mapHeight.ToString());

            this.add(widthUpButton);
            this.add(widthDownButton);
            this.add(heightUpButton);
            this.add(heightDownButton);
            this.add(widthText);
            this.add(heightText);


        }

        public void decrementHeight()
        {
            if (mapHeight > 1)
            {
                heightText.setString("Height: " + (--mapHeight).ToString());
            }
        }

        public void decrementWidth()
        {
            if (mapWidth > 1)
            {
                widthText.setString("Width:  "+ (--mapWidth).ToString());
            }
        }

        public void incrementHeight()
        {
            if (mapHeight < maxHeight)
            {
                heightText.setString("Height: " + (++mapHeight).ToString());
            }
        }

        public void incrementWidth()
        {
            if (mapWidth < maxWidth)
            {
                widthText.setString("Width:  " + (++mapWidth).ToString());
            }
        }

        public override void keyPressed(Keys key)
        {
            if (key == Keys.Back)
            {
                if (boardTitle.Length > 0)
                {
                    boardTitle = boardTitle.Remove(boardTitle.Length - 1);
                    refreshTitle();
                }
            }
            else if((key >= Keys.A && key <= Keys.Z) )
            {
                if (boardTitle.Length < maxTitleLength)
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift))
                    {
                        boardTitle += key;
                    }
                    else
                    {
                        boardTitle += key.ToString().ToLower();
                    }
                    refreshTitle();
                }
            }
            else if ((key >= Keys.D0 && key <= Keys.D9))
            {
                if (boardTitle.Length < maxTitleLength)
                {
                    boardTitle += key.ToString().ElementAt(1);
                    refreshTitle();
                }
            }
        }

        private void refreshTitle()
        {
            titleEditor.setString("Map Name: " + boardTitle);
        }

        internal void initializeMap()
        {
            map.setDimensions(mapWidth, mapHeight);
            map.setName(boardTitle);
        }
    }
}
